﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Helpers.Common
{
    public static class EnumarableToString
    {
        #region Properties

        private const string DEFAULT_SEPARATOR = ", ";
        private const string DEFAULT_KEY_VALUE_SEPARATOR = ": ";

        #endregion

        #region Methods

        public static string ToLog<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> iEnumerable)
        {
            return iEnumerable.ToString<TKey, TValue>(
                EnumarableToString.DEFAULT_SEPARATOR
                , EnumarableToString.DEFAULT_KEY_VALUE_SEPARATOR
            );
        }
        public static string ToLog<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> iEnumerable, string separator)
        {
            return iEnumerable.ToString<TKey, TValue>(
                separator
                , EnumarableToString.DEFAULT_KEY_VALUE_SEPARATOR
            );
        }
        public static string ToLog<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> iEnumerable, string separator, string keyValueSeparator)
        {
            return iEnumerable.ToString<TKey, TValue>(
                separator
                , keyValueSeparator
            );
        }
        public static string ToString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> iEnumerable, string separator)
        {
            return iEnumerable.ToString<TKey, TValue>(
                separator
                , EnumarableToString.DEFAULT_KEY_VALUE_SEPARATOR
            );
        }
        public static string ToString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> iEnumerable, string separator, string keyValueSeparator)
        {
            string valueString = "";

            if (iEnumerable.Count() != 0)
            {
                iEnumerable
                    .ToList()
                    .ForEach(kvp =>
                    {
                        valueString += string.Format(
                            "{0}{1}{2}{3}"
                            , kvp.Key
                            , keyValueSeparator
                            , HelperCommon.NullableObjectToTrimedString(kvp.Value)
                            , separator
                        );
                    });


                /* Remove that last separator */
                valueString = valueString.Substring(0, valueString.Length - separator.Length);
            }

            return valueString;
        }

        public static string ToLog<TItem>(this IEnumerable<TItem> iEnumerable)
        {
            return iEnumerable.ToString<TItem>(EnumarableToString.DEFAULT_SEPARATOR);
        }
        public static string ToLog<TItem>(this IEnumerable<TItem> iEnumerable, string separator)
        {
            return iEnumerable.ToString<TItem>(separator);
        }
        public static string ToString<TItem>(this IEnumerable<TItem> iEnumerable, string separator)
        {
            string valueString = "";

            if (iEnumerable.Count() != 0)
            {
                iEnumerable
                    .ToList()
                    .ForEach(item =>
                    {
                        valueString += string.Format(
                            "{0}{1}"
                            , HelperCommon.NullableObjectToTrimedString(item)
                            , separator
                        );
                    });


                /* Remove that last separator */
                valueString = valueString.Substring(0, valueString.Length - separator.Length);
            }

            return valueString;
        }

        public static string ToLog(this IEnumerable iEnumerable)
        {
            return iEnumerable.ToString(EnumarableToString.DEFAULT_SEPARATOR);
        }
        public static string ToLog(this IEnumerable iEnumerable, string separator)
        {
            return iEnumerable.ToString(separator);
        }
        public static string ToString(this IEnumerable iEnumerable, string separator)
        {
            string valueString = "";

            IEnumerator iEnum = iEnumerable.GetEnumerator();

            while (iEnum.MoveNext())
            {
                var item = iEnum.Current;

                valueString += string.Format(
                    "{0}{1}"
                    , HelperCommon.NullableObjectToTrimedString(item)
                    , separator
                );
            }

            if (valueString.Length > 0)
            {
                //Remove that last separator
                valueString = valueString.Substring(0, valueString.Length - separator.Length);
            }

            return valueString;
        }

        #endregion
    }
}